package io.github.junxworks.qt.modules.bi.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.sys.service.EpDataService;

@RestController
@RequestMapping("/bi/common/data")
public class CommonDataController {

	@Autowired
	private EpDataService dataService;

	/**
	 * Change stat daily.
	 *
	 * @param params 如下参数
	 * 	String sqlId; sql的id
	 	String table; 表名
	 	String orderBy; 排序条件
	 	Integer limit; 前几行
	 	String categoryField; 分类字段
	 	String valueField; value字段
	 	Integer unit;单位
	 * @return the result
	 * @throws Exception 
	 */
	@GetMapping("/table-filed")
	public Result changeStatDaily(@RequestParam Map<String, String> params) throws Exception {
		String sqlId = params.remove("sqlId");
		Result res = Result.ok();
		List<Map<String, Object>> l = dataService.getDataBySQLUid(sqlId, params);
		List<Object> category = Lists.newArrayList();
		List<Object> data = Lists.newArrayList();
		String categoryField = params.get("categoryField");
		String valueField = params.get("valueField");
		l.forEach(d -> {
			category.add(d.get(categoryField));
			data.add(d.get(valueField));
		});
		res.addAttribute("category", Lists.reverse(category));
		res.addAttribute("data", Lists.reverse(data));
		return res;
	}
}
